<template>
	<a-modal title="编辑物料" :width="1200" :visible="visible" :maskClosable="false" :confirmLoading="confirmLoading"
		:footer="extra.readonly" @ok="handleSubmit" @cancel="handleCancel">

		<a-spin :spinning="confirmLoading">
			<a-form :form="form">
				<a-row>

					<a-form-item style="display: none;" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
						<a-input v-decorator="['materialId']" />
					</a-form-item>

					<a-col :span="12">
						<a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入名称"
								v-decorator="['materialName', { rules: [{ required: true, message: '请输入名称！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col :span="12">
						<a-form-item label="料号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入料号"
								v-decorator="['materialCode', { rules: [{ required: true, message: '请输入料号！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col :span="12" v-show="false">
						<a-form-item label="是否常用" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-select :disabled="extra.readonly" placeholder="是否常用" v-decorator="['isCommon']">
								<a-select-option :value="value" v-for="{ label, value } in yesOrNoList">
									{{ label }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>

					<a-col :span="12" v-show="false">
						<a-form-item label="物料种类" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-select disabled v-decorator="['materialType', { rules: [{ required: true, message: '请输入物料种类！' }] }]">
								<a-select-option :value="id" v-for="{ id, name } in $businessEnum.MATERIAL_TYPE_ENUM">
									{{ name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>



					<a-col :span="12" v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.packing.id">
						<a-form-item label="尺寸" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input placeholder="请输入尺寸" v-decorator="['extend1', { rules: [{ required: true, message: '请输入尺寸！' }] }]"
								:disabled="extra.readonly" />
						</a-form-item>
					</a-col>
					<a-col :span="12"
						v-if="(canReadOrgIdCompany() && materialType == '1473944715475824641') || canReadOrgIdCompany1()">
						<a-form-item label="归属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							
							<a-select v-decorator="['orgIdCompany', { rules: [{ required: true, message: '请输入归属公司！' }] }]"
								:disabled="extra.readonly" placeholder="请输入归属子公司">
								<a-select-option :value="id" v-for="{ id, name } in companyList">
									{{ name }}
								</a-select-option>
							</a-select>
							<!--              <a-tree-select v-decorator="['orgIdCompany', {rules: [{required: true, message: '请输入归属公司！'}]}]"-->
							<!--                             :disabled="extra.readonly" :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"-->
							<!--                             :treeData="orgTree"-->
							<!--                             placeholder="请输入归属子公司"-->
							<!--                             style="width: 100%"-->
							<!--                             treeDefaultExpandAll-->
							<!--              >-->
							<!--                <span slot="title" slot-scope="{ id }">{{ id }}</span>-->
							<!--              </a-tree-select>-->
						</a-form-item>
					</a-col>

					<a-col :span="12" class="form-item :disabled=extra.readonly-placeholder"
						v-if="canReadSupplier && canReadOrgIdCompany()">
						<a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<!--              <a-tree-select :disabled=extra.readonly placeholder="请输入供应商" v-decorator="['orgIdSupplier', {rules: [{required: true, message: '请输入供应商！'}]}]"-->
							<!--                             style="width: 100%"-->
							<!--                             :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"-->
							<!--                             :treeData="orgTree"-->
							<!--                             treeDefaultExpandAll-->
							<!--              >-->
							<!--                <span slot="title" slot-scope="{ id }">{{ id }}</span>-->
							<!--              </a-tree-select>-->
							<a-select :disabled="extra.readonly" placeholder="请输入供应商"
								v-decorator="['orgIdSupplier', { rules: [{ required: true, message: '请输入供应商！' }] }]"
								@change="handleSupplierChange">
								<a-select-option :value="id" v-for="{ id, name } in suppliers" :key="id">
									{{ name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<!-- 如果价格有值则不可编辑，直接回显 -->
					<!-- 如果不是财务或者总经理，也不给编辑价格 -->
					<!-- {{ firstPrice }}
					 {{ (!hasRoleNoAdmin(['general manager', 'caiwu'])) }}
					 {{ (isJSYRole() && materialType == '1843830985384407041') }} -->
					<template v-if="firstPrice">
						<a-col :span="12" v-if="managerOrFinance() || (isJSYRole() && materialType == '1843830985384407041')">
							<!-- 22--{{ firstPrice }} -->
							<!-- v-decorator="['purchasePrice', {rules: [{required: true, message: '请输入！'}]}]" -->
							<a-form-item label="采购价(含税)" v-show="extra.showPrice !== '0'" :labelCol="labelCol"
								:wrapperCol="wrapperCol" has-feedback="">
								<a-input-number :min="0" style="width: 100%" disabled placeholder="请输入"
									v-decorator="['purchasePrice', { rules: [{ required: true, message: '请输入！' }] }]"
									v-model="purchasePrice" />
							</a-form-item>
						</a-col>
						<a-col :span="12" v-if="managerOrFinance() || (isJSYRole() && materialType == '1843830985384407041')">
							<a-form-item label="成本价(不含税)" v-show="extra.showPrice !== '0'" :labelCol="labelCol"
								:wrapperCol="wrapperCol" has-feedback="">
								<a-input-number :min="0" disabled style="width: 100%" placeholder="空"
									v-decorator="['costPrice', { rules: [{ required: true, message: '空！' }] }]" />
							</a-form-item>
						</a-col>
					</template>
					<!-- 如果价格为0则可以编辑 -->
					<template v-else>
						<a-col :span="12" v-if="managerOrFinance() || (isJSYRole() && materialType == '1843830985384407041')">
							<!-- v-decorator="['purchasePrice', {rules: [{required: true, message: '请输入！'}]}]" -->
							<a-form-item label="采购价(含税)" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
								<a-input-number :min="0" style="width: 100%" placeholder="请输入"
									v-decorator="['purchasePrice', { rules: [{ required: true, message: '请输入！' }] }]"
									v-model="purchasePrice" />
							</a-form-item>
						</a-col>
						<a-col :span="12" v-if="managerOrFinance() || (isJSYRole() && materialType == '1843830985384407041')">
							<a-form-item label="成本价(不含税)" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
								<a-input-number :min="0" disabled style="width: 100%" placeholder="根据采购价计算而来" :value="costPrice" />
							</a-form-item>
						</a-col>
					</template>
					<a-col :span="12">
						<a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入规格"
								v-decorator="['materialModelNumber', { rules: [{ required: true, message: '请输入规格！' }] }]" />
						</a-form-item>
					</a-col>



					<a-col :span="12">
						<a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-select @focus="getdanwei" :disabled="extra.readonly" placeholder="请选择"
								v-decorator="['unit', { rules: [{ required: true, message: '请选择单位！' }] }]">
								<a-select-option :value="item.id" v-for="(item, index) in danweiList" :key="index">{{ item.value
									}}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>

					<a-col :span="12">
						<a-form-item label="库存数量" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input-number :min="0" style="width: 100%" disabled placeholder="请输入库存数量" v-decorator="['inventoryNumber',
								{ rules: [{ required: true, message: '请输入库存数量！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col :span="12">
						<a-form-item label="预警值" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input-number :min="0" style="width: 100%" :disabled="extra.readonly" placeholder="请输入预警值" v-decorator="['inventoryWarningValue',
								{ rules: [{ required: true, message: '请输入预警值！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col :span="12">
						<a-form-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-select v-decorator="['orgIdStorehouse', { rules: [{ required: true, message: '请输入仓库！' }] }]"
								:disabled="extra.readonly" placeholder="请输入仓库" @change="handleOrgIdStorehousePositionChange">
								<a-select-option :value="id" v-for="{ id, name } in storehouseList" :key="id">
									{{ name }}
								</a-select-option>
							</a-select>
							<!--              <a-tree-select :disabled="extra.readonly" placeholder="请输入仓库" v-decorator="['orgIdStorehouse', {rules: [{required: true, message: '请输入仓库！'}]}]"-->
							<!--                             style="width: 100%"-->
							<!--                             :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"-->
							<!--                             :treeData="orgTree"-->
							<!--                             treeDefaultExpandAll-->
							<!--              >-->
							<!--                <span slot="title" slot-scope="{ id }">{{ id }}</span>-->
							<!--              </a-tree-select>-->
						</a-form-item>
					</a-col>

					<a-col :span="12">
						<a-form-item label="库位" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-select v-decorator="['orgIdStorehousePosition', { rules: [{ required: true, message: '请输入库位！' }] }]"
								:disabled="extra.readonly" placeholder="请输入库位">
								<a-select-option :value="id" v-for="{ id, name } in storehousePositionList">
									{{ name }}
								</a-select-option>
							</a-select>
							<!--              <a-tree-select :disabled="extra.readonly" placeholder="请输入库位" v-decorator="['orgIdStorehousePosition', {rules: [{required: true, message: '请输入库位！'}]}]"-->
							<!--                             style="width: 100%"-->
							<!--                             :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"-->
							<!--                             :treeData="orgTree"-->
							<!--                             treeDefaultExpandAll-->
							<!--              >-->
							<!--                <span slot="title" slot-scope="{ id }">{{ id }}</span>-->
							<!--              </a-tree-select>-->
						</a-form-item>
					</a-col>

					<a-col v-show="false" :span="12"
						v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.packing.id || materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<a-form-item label="每次采购数量" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入每次采购数量" v-decorator="['singlePurchaseQuantity']"
								:min="0" style="width: 100%" />
						</a-form-item>
					</a-col>

					<a-col v-show="false" :span="12"
						v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.packing.id || materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<a-form-item label="供应商欠货数量" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入供应商欠货数量" v-decorator="['owedQuantity']" :min="0"
								style="width: 100%" />
						</a-form-item>
					</a-col>

					<a-col :span="12" v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<a-form-item label="材质" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入材质"
								v-decorator="['extend1', { rules: [{ required: true, message: '请输入材质！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col v-show="false" :span="12" v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<a-form-item label="分类" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入分类" v-decorator="['extend2']" />
						</a-form-item>
					</a-col>

					<a-col :span="12" v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<a-form-item label="颜色" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<!-- <a-input :disabled="extra.readonly" placeholder="请输入颜色"
								v-decorator="['extend3', { rules: [{ required: true, message: '请输入颜色！' }] }]" /> -->
							<!-- <ChromePicker ></ChromePicker> -->
							<!-- <span style="margin: 0px 10px;font-weight: bold;" :style="{ color: extend3 }">{{ extend3 }}</span> -->
							<input :disabled="extra.readonly" type="color" v-decorator="['extend3', { rules: [{ required: true, message: '请输入颜色！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col v-show="false" :span="12" v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入规格" v-decorator="['extend4']" />
						</a-form-item>
					</a-col>

					<a-col :span="12" v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<a-form-item label="米重" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入米重"
								v-decorator="['extend5', { rules: [{ required: true, message: '请输入米重！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col :span="12" v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<a-form-item label="外径" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入外径"
								v-decorator="['extend6', { rules: [{ required: true, message: '请输入外径！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col v-show="false" :span="12" v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<a-form-item label="是否客户定制" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-select placeholder="请输入是否客户定制" v-decorator="['extend7']" :disabled="extra.readonly">
								<a-select-option :value="value" v-for="{ label, value } in yesOrNoList">
									{{ label }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>

					<a-col :span="24">
						<a-form-item label="图片" :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" has-feedback="">
							<a-input v-show="false" :disabled="extra.readonly" placeholder="请上传图片"
								v-decorator="['image', { rules: [{ required: true, message: '请上传图片！' }] }]" />
							<fc-upload :file-list="fileList" :disabled="extra.readonly"
								:handle-file-list-string-change="handleFileListStringChange" />
							<!-- 这里是编辑 -->
							<!-- <CropperUpload
              :file-list="fileList"
              :disabled="extra.readonly"
              @fcsuccess="fcsuccess"
              avatarText=""
            /> -->
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="领用方" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-select placeholder="请选择" :disabled="extra.readonly" allowClear
								v-decorator="['classGroupId', { rules: [{ required: true, message: '请选择领用方！' }] }]">
								<a-select-option :value="item.classGroupId" v-for="(item, index) in teamList" :key="index">
									{{ item.name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input-number :min="0" :disabled="extra.readonly" placeholder="请输入排序" style="width: 100%;"
								v-decorator="['sort', { rules: [{ required: true, message: '请输入排序！' }] }]" />
						</a-form-item>
					</a-col>
					<!-- <a-col :span="24" style="display: none;">
						<a-form-item label="领用方name" :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" has-feedback="">
							<a-select placeholder="请选择" :disabled="extra.readonly" style="width: 40%;" allowClear @change="teamChange"
								v-decorator="['classGroupName', { rules: [{ required: true, message: '请选择领用方！' }] }]">
								<a-select-option :value="item.classGroupId" v-for="(item, index) in teamList" :key="index">
									{{ item.name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col> -->
					<a-col :span="24">
						<a-form-item label="描述" :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" has-feedback="">
							<a-textarea v-decorator="['description', { rules: [{ required: true, message: '请输入描述！' }] }]"
								:disabled="extra.readonly" placeholder="请输入内容" :auto-size="{ minRows: 3, maxRows: 5 }" />
						</a-form-item>
					</a-col>

					<a-col :span="12" v-show="false">
						<a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入创建时间"
								v-decorator="['createTime', { rules: [{ required: false, message: '请输入创建时间！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col :span="12" v-show="false">
						<a-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入创建人"
								v-decorator="['createUser', { rules: [{ required: false, message: '请输入创建人！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col :span="12" v-show="false">
						<a-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入更新时间"
								v-decorator="['updateTime', { rules: [{ required: false, message: '请输入更新时间！' }] }]" />
						</a-form-item>
					</a-col>

					<a-col :span="12" v-show="false">
						<a-form-item label="更新人" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input :disabled="extra.readonly" placeholder="请输入更新人"
								v-decorator="['updateUser', { rules: [{ required: false, message: '请输入更新人！' }] }]" />
						</a-form-item>
					</a-col>

				</a-row>
			</a-form>
		</a-spin>
	</a-modal>
</template>

<script>
import { edit } from '@/api/modular/main/emsmaterial/emsMaterialManage'
import { sysFileInfoUpload } from '@/api/modular/system/fileManage'
import { getOrgList, getOrgTree } from '@/api/modular/system/orgManage'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import FcUpload from '@/components/FCUpload/FcUpload'
import { list as emsOrgList } from '@/api/modular/main/emsorg/emsOrgManage'
import { hasRoleNoAdmin } from '@/utils/permissions'

import { page as teamPage } from '@/api/modular/main/classGroup/classGroup'

export default {
	components: { FcUpload },
	mounted() {
		this.getdanwei()
	},
	data() {
		return {
			labelCol: {
				xs: { span: 24 },
				sm: { span: 8 }
			},
			wrapperCol: {
				xs: { span: 24 },
				sm: { span: 15 }
			},
			labelColBlock: {
				xs: { span: 24 },
				sm: { span: 4 }
			},
			wrapperColBlock: {
				xs: { span: 24 },
				sm: { span: 18 }
			},
			visible: false,
			confirmLoading: false,
			form: this.$form.createForm(this),
			loading: false,
			isPackaging: false,
			needDeleteFileUid: null,
			previewVisible: false,
			previewImage: '',
			fileList: [],
			orgIdStorehouse: null,
			sysOrgs: [],
			materialType: '',
			orgTree: [],
			// suppliers: [],
			extra: {},
			danweiList: [],
			purchasePrice: undefined,
			firstPrice: null,
			fcData: {
				taxRate: '',  //税率
				invoiceType: '', //1普票  2专票
				isInvoice: '',  //1不需要  2需要开票
				unitRanId: '', //供应商类型     1440879787235438593主要供应商     1498270916314161153非主要供应商
			},
			teamList: [],
		}
	},
	computed: {
		costPrice() {
			// 主要供应商
			if (this.fcData.unitRanId == '1440879787235438593') {
				return Number(((this.purchasePrice || 0) / (1 + (this.fcData.taxRate && this.fcData.isInvoice == 2 && this.fcData.invoiceType == 2 ? this.fcData.taxRate : 0) / 100)).toFixed(2))
			} else {
				return this.purchasePrice
			}
		},

		/**
		 * 公司下拉菜单
		 * @returns {{label: string, value: string}[]|*[]}
		 */
		companyList() {
			if (this.sysOrgs.length == 0) {
				return []
			}
			return this.sysOrgs.filter((org) => org.unitRanId == this.$businessEnum.ORG_TYPE_ENUM.COMPANY.id).map((org) => {
				return { id: org.id + '', name: org.name }
			})
		},
		/**
		 * 供应商下拉菜单
		 * @returns {{label: string, value: string}[]|*[]}
		 */
		suppliers() {
			return this.sysOrgs.filter(sysOrg => {
				return sysOrg.unitRanId == this.$businessEnum.ORG_TYPE_ENUM.PRIMARY_SUPPLIER.id
					|| sysOrg.unitRanId == this.$businessEnum.ORG_TYPE_ENUM.NORMAL_SUPPLIER.id
			})
		},
		/**
		 * 仓库
		 * @returns {{label: string, value: string}[]|*[]}
		 */
		storehouseList() {
			if (this.sysOrgs.length == 0) {
				return []
			}
			return this.sysOrgs.filter((org) => org.unitRanId == this.$businessEnum.ORG_TYPE_ENUM.STOREHOUSE.id && org.materialType == this.materialType).map((org) => {
				return { id: org.id + '', name: org.name }
			})
		},
		/**
		 * 库位
		 * @returns {{label: string, value: string}[]|*[]}
		 */
		storehousePositionList() {
			if (this.sysOrgs.length == 0) {
				return []
			}
			const orgTypeId = this.$businessEnum.ORG_TYPE_ENUM.STOREHOUSE_POSITION.id
			const storehouseId = this.orgIdStorehouse
			return this.sysOrgs.filter((org) => {
				return org.unitRanId == orgTypeId && org.pid == storehouseId
			}).map((org) => {
				return { id: org.id + '', name: org.name }
			})
		},
		yesOrNoList() {
			return [
				{ label: '是', value: 'Y' },
				{ label: '否', value: 'N' },
			]
		},
	},
	methods: {
		fcsuccess(url) {
			this.form.setFieldsValue({ image: url })
		},
		// 供应商change事件
		handleSupplierChange(value) {
			console.log('@@@', this.suppliers)
			let obj = this.suppliers.find(item => item.id == value)
			this.fcData = {
				isInvoice: obj.isInvoice || '',
				invoiceType: obj.invoiceType || '',
				taxRate: obj.taxRate || '',
				unitRanId: obj.unitRanId || '',
			}
		},
		/**
		 * 获取领用方
		 */
		getTeamList() {
			teamPage({
				pageSize: -1,
				status: 0
			}).then(res => {
				this.teamList = res.data.rows;
			})
		},
		/**
		 * 领用方切换
		 * @param e 
		 */
		teamChange(e) {
			console.log(e);
			let classGroupName = null
			this.teamList.forEach(val => {
				if (val.classGroupId == e) {
					classGroupName = val.name;
				}
			})
			setTimeout(() => {
				this.form.setFieldsValue({
					classGroupName: classGroupName
				})
			}, 200);
		},
		getdanwei() {
			this.axios.get('/sysDictData/list?typeCode=danwei').then((res) => {
				this.danweiList = res.data
				console.log(this.danweiList)
			})
		},
		canReadSupplier() {
			return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN2.id)
		},
		//GS_ADMIN：集团子公司
		//MATERIAL_ADMIN3：线缆仓库管理员
		canReadOrgIdCompany() {
			return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
				// && !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN2.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.SQCG.id)
		},

		// 总经理
		canReadOrgIdCompany1() {
			return this.hasRole(this.$util.ROLE_ENUM.GENERALMANAGER.id)
		},
		edit(record, extra) {
			console.log("record", record);

			this.visible = true
			this.firstPrice = record.purchasePrice
			this.getOrgTree()
			this.extra = extra || {}
			this.materialType = record.materialType
			this.$nextTick(() => {
				setTimeout(() => {
					this.form.setFieldsValue(record)
					this.orgIdStorehouse = record.orgIdStorehouse
				}, 100)
				console.log(record.image);
				console.log("this.$store.state.baseUrl", this.$store.state.baseUrl);

				this.fileList = record.image.split(',').map(fileID => {
					return {
						uid: fileID,
						name: fileID,
						status: 'done',
						url: `${this.$store.state.baseUrl}/sysFileInfo/preview?id=${fileID}&token=${token}`,
						fileID: fileID
					}
				})
			})
			// 回显fcData
			this.fcData = {
				isInvoice: record.isInvoice || '',
				invoiceType: record.invoiceType || '',
				taxRate: record.taxRate || '',
				unitRanId: record.unitRanId || '',
			}

			this.getSysOrgs()
			this.getTeamList();
			// this.getSuppliers()
			// 还需要将数据中的图片格式化
			const token = Vue.ls.get(ACCESS_TOKEN)
		},
		handleSubmit() {
			const { form: { validateFields } } = this
			this.confirmLoading = true

			validateFields((errors, values) => {
				if (!errors) {
					let query = {}
					if (this.firstPrice) {
						query = {
							...values
						}
					} else {
						query = {
							...values,
							costPrice: this.costPrice
						}
					}
					edit(query).then((res) => {
						this.confirmLoading = false
						if (res.success) {
							this.$message.success('编辑成功')
							this.handleCancel()
							this.$emit('ok', values)
						} else {
							this.$message.info('编辑失败：' + res.message)
						}
					}).finally((res) => {
						this.confirmLoading = false
					})
				} else {
					this.confirmLoading = false
				}
			})
		},
		handleCancel() {
			this.form.resetFields()
			this.firstPrice = null
			this.costPrice = null
			this.visible = false
		},
		beforeUpload(file) {
			const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
			if (!isJpgOrPng) {
				this.$message.error('只能上传图片类型文件')
			}
			const isLt2M = file.size / 1024 / 1024 < 20
			if (!isLt2M) {
				this.$message.error('文件大小不可超过 20MB!')
			}
			if (!(isJpgOrPng && isLt2M)) {
				// 需要从列表中删除这次上传的文件
				this.needDeleteFileUid = file.uid
			}
			return isJpgOrPng && isLt2M
		},
		handleChange({ file, fileList }) {
			this.fileList = fileList
			// 如果在上传前判断格式或大小不符合，将不会进行自动上传。这处的状态为undefined
			if (file.status === undefined) {
				fileList.pop()
			}
			let image = this.fileList.map(value => value.fileID).join(',')
			this.form.setFieldsValue({ image })
		},
		customRequest(data) {
			const formData = new FormData()
			formData.append('file', data.file)
			sysFileInfoUpload(formData).then((res) => {
				if (res.success) {
					this.$message.success('上传成功')

					const token = Vue.ls.get(ACCESS_TOKEN)

					let _fl = this.fileList
					let lastFile = _fl[_fl.length - 1]
					let imageUrl = `${this.$store.state.baseUrl}/sysFileInfo/preview?id=${res.data}&token=${token}`
					// 为了节省流量，上传成功的图片预览图直接使用文件的thumbUrl
					imageUrl = lastFile.thumbUrl
					lastFile.fileID = res.data
					lastFile.url = ''
					lastFile.status = 'done'

					let image = _fl.map(value => value.fileID).join(',')
					this.form.setFieldsValue({ image })
				} else {
					this.$message.error('上传失败：' + res.message)
				}
			})
		},
		async handlePreview(file) {
			this.previewImage = file.url || file.preview || `${this.$store.state.baseUrl}/sysFileInfo/preview?id=${file.fileID}`
			this.previewVisible = true
		},
		handlePreviewCancel() {
			this.previewVisible = false
			this.previewImage = ''
		},
		afterUpload(isSuccess, res, fileList) {
			console.log(`afterUpload`)
			if (isSuccess) {
				this.fileList = fileList
				let image = this.fileList.map(value => value.fileID).join(',')
				this.form.setFieldsValue({ image })
			} else {
				console.error(`上传报错:${res.message}`)
			}
		},
		afterHandleChange(fileList) {
			console.log(`edit afterHandleChange`)
			this.fileList = fileList
			let image = this.fileList.map(value => value.fileID).join(',')
			this.form.setFieldsValue({ image })
		},
		handleFileListStringChange(fileListString) {
			// console.debug(`fileListString: ${fileListString}`)
			this.form.setFieldsValue({ image: fileListString })
		},
		getSysOrgs() {
			// 传统的机构获取方法,有很强的数据范围约束.需要配置相应的角色数据范围才能查询到.先用下面这种简单的方法
			// getOrgList({
			//   // dataScope: '1473299760817713154'
			// }).then((res) => {
			//   this.sysOrgs = res.data
			// })


			emsOrgList().then((res) => {
				this.sysOrgs = res.data
			})
		},
		getOrgTree() {
			// 如果是总经理登录的话，就请求这个接口
			if (this.canReadOrgIdCompany1()) {
				getOrgTree().then((res) => {
					this.formLoading = false
					if (!res.success) {
						this.orgTree = []
						return
					}
					this.orgTree = [{
						'id': '-1',
						'parentId': '0',
						'title': '顶级',
						'value': '0',
						'pid': '0',
						'children': res.data
					}]
				})
			}
		},
		getSuppliers() {
			emsOrgList({ pid: '1473297518794452994', unitRanId: this.$businessEnum.ORG_TYPE_ENUM.PRIMARY_SUPPLIER.id }).then((res) => {
				this.suppliers = res.data
			})
		},
		handleOrgIdStorehousePositionChange(value) {
			// 赋值,用于库位级联
			this.orgIdStorehouse = value
			// 清他位值
			this.form.setFieldsValue({ orgIdStorehousePosition: null })
		}
	}
}
</script>
